package models

import "time"
import "gorm.io/gorm"
import "go_part/utils"
//import "fmt"

type LevelLocal struct{
	gorm.Model
	Name string
	LevelLocalID string
	ParentLocalID string
	LevelTypeID string
	CompanyID string
	Phone string `valid:"matches(^1[3-9]{1}\\d{9}$)"`
	Email string `valid:"email"`
	Identity string
	ClientIP string
	ClientPort string
	Salt string
	LoginTime time.Time
	isLogout bool
	DeviceInfo string
}

func (table *LevelLocal) TableName() string {

	return "level_local_basic"
}

func GetLevelLocalList() []*LevelLocal { 
	data := make([]*LevelLocal,10)
	utils.DB.Find(&data)
	// for _,v : = range data {
	// 	fmt.Println(v)
	// }
	return data
}

func FindLevelLocalByName(name string) LevelLocal {
	user := LevelLocal{}
	utils.DB.Where("name = ?",name).Find(&user)
	return user
}

func FindLevelLocalByID(id uint) LevelLocal {
	user := LevelLocal{}
	utils.DB.Where("id = ?",id).First(&user)
	return user
}

func FindLevelLocalByIDAndCompanyID(id string,CompanyID string) LevelLocal {
	user := LevelLocal{}
	utils.DB.Where("level_local_id = ? and company_id = ?",id,CompanyID).First(&user)
	return user
}

func FindLevelLocalByTypeIDAndCompanyID(id string,CompanyID string) []*LevelLocal {
	user := make([]*LevelLocal,100)
	utils.DB.Where("level_type_id = ? and company_id = ?",id,CompanyID).Find(&user)
	return user
}

func CreateLevelLocal(user LevelLocal) *gorm.DB {
	
	return utils.DB.Create(&user)
}

func DeleteLevelLocal(user LevelLocal) *gorm.DB {
	
	return utils.DB.Delete(&user)
}

func UpdateLevelLocal(user LevelLocal) *gorm.DB {
	
	return utils.DB.Model(&user).Updates(LevelLocal{Name: user.Name,DeviceInfo: user.DeviceInfo})
}